package com.panfeng.xcloud.common.core.thread;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * 本地线程变量集合Map
 *
 * @author xiaobo
 * @version 1.0
 * @since 2018-12-28
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ThreadLocalMap {

	private final static ThreadLocal<Map<String, Object>> THREAD_CONTEXT = new MapThreadLocal();

	public static void put(String key, Object value) {
		getContextMap().put(key, value);
	}

	public static Object remove(String key) {
		return getContextMap().remove(key);
	}

	public static Object get(String key) {
		return getContextMap().get(key);
	}

	private static class MapThreadLocal extends ThreadLocal<Map<String, Object>> {

		@Override
		protected Map<String, Object> initialValue() {
			return new HashMap<String, Object>() {

				private static final long serialVersionUID = 3637958959138295593L;

				@Override
				public Object put(String key, Object value) {
					return super.put(key, value);
				}
			};
		}
	}

	private static Map<String, Object> getContextMap() {
		return THREAD_CONTEXT.get();
	}

	public static void remove() {
		getContextMap().clear();
	}
}
